using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    public partial class StrategicUBoatWarfareDialog : Form
    {
        private int numUBoats;
        private int ASWLevel;
        private int numASWHits = 0;
        private int numUKWERPs;
        private int numWERPHits = 0;
        private Bitmap crossHairs;
        private Random r;

        public StrategicUBoatWarfareDialog(int numUBoats, int ASWLevel, int numUKWERPs, Random r)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Target.bmp");
            crossHairs = (Bitmap)Bitmap.FromStream(file);
            crossHairs.MakeTransparent(System.Drawing.Color.White);
            file.Dispose();

            InitializeComponent();
            this.numUBoats = numUBoats;
            this.ASWLevel = ASWLevel;
            this.numUKWERPs = numUKWERPs;
            this.r = r;
            ResolveUBoatWarfare();
        }

        public StrategicUBoatWarfareDialog(int numUBoats, int ASWLevel, int numUKWERPs, int numASWHits, int numWERPHits)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Target.bmp");
            crossHairs = (Bitmap)Bitmap.FromStream(file);
            crossHairs.MakeTransparent(System.Drawing.Color.White);
            file.Dispose();

            InitializeComponent();
            this.numUBoats = numUBoats;
            this.ASWLevel = ASWLevel;
            this.numUKWERPs = numUKWERPs;
            this.numASWHits = numASWHits;
            this.numWERPHits = numWERPHits;
            this.numberOfASWLabel.Text = this.ASWLevel.ToString();
            this.numUBoatsFiredUponLabel.Text = this.numUBoats.ToString();
            this.numUBoatsLeftLabel.Text = (this.numUBoats - this.numASWHits).ToString();
            this.numUBoatsFiringLabel.Text = (this.numUBoats - this.numASWHits).ToString();
            this.numUKWERPSLabel.Text = this.numUKWERPs.ToString();
            this.numUKWERPSLeftLabel.Text = (this.numUKWERPs - this.numWERPHits).ToString();
            this.numASWHitsLabel.Text = this.numASWHits.ToString();
            this.numUBoatHitsLabel.Text = this.numWERPHits.ToString();
        }

        private void ResolveUBoatWarfare()
        {
            uint dieRoll;

            if(ASWLevel > 0)
            {
                int percentageLoss = 0;
                dieRoll = (uint)(r.Next(6) + 1);
                switch(dieRoll)
                {
                    case 1:
                        switch(ASWLevel)
                        {
                            case 5:
                            case 6:
                                percentageLoss = 5;
                                break;
                            case 7:
                                percentageLoss = 10;
                                break;
                        }
                        break;
                    case 2:
                        switch(ASWLevel)
                        {
                            case 4:
                            case 5:
                                percentageLoss = 5;
                                break;
                            case 6:
                            case 7:
                                percentageLoss = 10;
                                break;
                        }
                        break;
                    case 3:
                        switch(ASWLevel)
                        {
                            case 3:
                            case 4:
                                percentageLoss = 5;
                                break;
                            case 5:
                            case 6:
                                percentageLoss = 10;
                                break;
                            case 7:
                                percentageLoss = 15;
                                break;
                        }
                        break;
                    case 4:
                        switch (ASWLevel)
                        {
                            case 2:
                            case 3:
                                percentageLoss = 5;
                                break;
                            case 4:
                            case 5:
                                percentageLoss = 10;
                                break;
                            case 6:
                                percentageLoss = 15;
                                break;
                            case 7:
                                percentageLoss = 20;
                                break;
                        }
                        break;
                    case 5:
                        switch(ASWLevel)
                        {
                            case 1:
                            case 2:
                                percentageLoss = 5;
                                break;
                            case 3:
                            case 4:
                                percentageLoss = 10;
                                break;
                            case 5:
                                percentageLoss = 15;
                                break;
                            case 6:
                                percentageLoss = 20;
                                break;
                            case 7:
                                percentageLoss = 25;
                                break;
                        }
                        break;
                    case 6:
                        switch(ASWLevel)
                        {
                            case 1:
                                percentageLoss = 5;
                                break;
                            case 2:
                            case 3:
                                percentageLoss = 10;
                                break;
                            case 4:
                                percentageLoss = 15;
                                break;
                            case 5:
                                percentageLoss = 20;
                                break;
                            case 6:
                            case 7:
                                percentageLoss = 25;
                                break;
                        }
                        break;
                }

                numASWHits = (int)Math.Ceiling((((float)percentageLoss) / 100) * (float)numUBoats);
            }

            if((numUBoats - numASWHits) > 0)
            {
                dieRoll = (uint)(r.Next(6) + 1);
                switch((numUBoats - numASWHits))
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        switch(dieRoll)
                        {
                            case 4:
                            case 5:
                                numWERPHits = 1;
                                break;
                            case 6:
                                numWERPHits = 2;
                                break;
                        }
                        break;
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                        switch(dieRoll)
                        {
                            case 3:
                            case 4:
                                numWERPHits = 1;
                                break;
                            case 5:
                                numWERPHits = 2;
                                break;
                            case 6:
                                numWERPHits = 3;
                                break;
                        }
                        break;
                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 17:
                    case 18:
                    case 19:
                    case 20:
                        switch(dieRoll)
                        {
                            case 1:
                                numWERPHits = 1;
                                break;
                            case 2:
                                numWERPHits = 2;
                                break;
                            case 3:
                                numWERPHits = 3;
                                break;
                            case 4:
                            case 5:
                                numWERPHits = 4;
                                break;
                            case 6:
                                numWERPHits = 5;
                                break;
                        }
                        break;
                    case 21:
                    case 22:
                    case 23:
                    case 24:
                    case 25:
                    case 26:
                    case 27:
                    case 28:
                    case 29:
                    case 30:
                        switch(dieRoll)
                        {
                            case 1:
                                numWERPHits = 2;
                                break;
                            case 2:
                                numWERPHits = 3;
                                break;
                            case 3:
                            case 4:
                                numWERPHits = 4;
                                break;
                            case 5:
                                numWERPHits = 5;
                                break;
                            case 6:
                                numWERPHits = 6;
                                break;
                        }
                        break;
                    case 31:
                    case 32:
                    case 33:
                    case 34:
                    case 35:
                    case 36:
                    case 37:
                    case 38:
                    case 39:
                    case 40:
                    case 41:
                    case 42:
                    case 43:
                    case 44:
                    case 45:
                    case 46:
                    case 47:
                    case 48:
                    case 49:
                    case 50:
                        switch(dieRoll)
                        {
                            case 1:
                                numWERPHits = 4;
                                break;
                            case 2:
                                numWERPHits = 6;
                                break;
                            case 3:
                                numWERPHits = 7;
                                break;
                            case 4:
                                numWERPHits = 8;
                                break;
                            case 5:
                                numWERPHits = 9;
                                break;
                            case 6:
                                numWERPHits = 11;
                                break;
                        }
                        break;
                    default:
                        switch(dieRoll)
                        {
                            case 1:
                                numWERPHits = 8;
                                break;
                            case 2:
                                numWERPHits = 10;
                                break;
                            case 3:
                                numWERPHits = 12;
                                break;
                            case 4:
                                numWERPHits = 14;
                                break;
                            case 5:
                                numWERPHits = 15;
                                break;
                            case 6:
                                numWERPHits = 16;
                                break;
                        }
                        break;
                }
            }

            this.numberOfASWLabel.Text = this.ASWLevel.ToString();
            this.numUBoatsFiredUponLabel.Text = this.numUBoats.ToString();
            this.numUBoatsLeftLabel.Text = (this.numUBoats - this.numASWHits).ToString();
            this.numUBoatsFiringLabel.Text = (this.numUBoats - this.numASWHits).ToString();
            this.numUKWERPSLabel.Text = this.numUKWERPs.ToString();
            this.numUKWERPSLeftLabel.Text = (this.numUKWERPs - this.numWERPHits).ToString();
            this.numASWHitsLabel.Text = this.numASWHits.ToString();
            this.numUBoatHitsLabel.Text = this.numWERPHits.ToString();
        }

        public int UBoatLosses
        {
            get
            {
                return numASWHits;
            }
        }

        public int WERPLosses
        {
            get
            {
                return numWERPHits;
            }
        }

        private void UBoatsFiredUponPictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(crossHairs, new Rectangle(0, 0, UBoatsFiredUponPictureBox.Width, UBoatsFiredUponPictureBox.Height));
        }

        private void UKWERPSpictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(crossHairs, new Rectangle(0, 0, UKWERPSpictureBox.Width, UKWERPSpictureBox.Height));
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}